﻿<?php
include_once("../initial.php");
include_once(constant("PROJECT_DIR")."/dbdriver/orderWHandle.php");
include_once(constant("PROJECT_DIR")."/dbdriver/orderRHandle.php");
include_once(constant("PROJECT_DIR")."/dbdriver/courseRHandle.php");
include_once(constant("PROJECT_DIR")."/dbdriver/userRHandle.php");
include_once(constant("PROJECT_DIR")."/dbdriver/userWHandle.php");
class orderLogic
{
		public function getCoursesByUid($uid)
		{
				if($uid=='')
				{
						return null;
				}
				$cl = new courseLogic();
		}
    public function insert_order($array)
    {
        if(is_array($array)==false)
        {
            return false;
        }
        if(count($array)==0)
        {
            return false;
        }
        $whandle = new orderWHandle();
        return $whandle->insert_data($array);
    }
    public function get_order($array='', $orderby='', $limit='')
    {
        $rhandle = new orderRHandle();
        if($array == '')
        {
            return $rhandle->get_all_data($orderby,$limit);
        }
        else
        {
            return $rhandle->get_some_data($array, $orderby, $limit);
        }
    }
    public function get_order_num($array='')
    {
        $rhandle = new orderRHandle();
        return $rhandle->get_order_num($array);
    }
    public function update_order($array,$id)
    {
        $handle = new orderWHandle();
        return $handle->update_order($array,$id);
    }
    public function delete_order($id)
    {
        $handle = new orderWHandle();
        return $handle->delete_order($id);
    }
    public function getUserCourses($uid)
    {
    		$ch = new userRHandle();
    		return $ch->get_user_courses(array('uid'=>$uid));
    }
    public function searchDevice($uid, $cid, $type='')
    {
    		if($uid=='' || $cid=='')
    		{
    			return array();
    		}
    		$or = new orderRHandle();
    		return $or->searchDevice($uid, $cid, $type);
    }
    public function checkRemain($did, $beginDate, $endDate)
    {
    		if($did=='' || $beginDate>$endDate || $beginDate=='' || $endDate=='')
    		{
    				return false;
    		}
    		$or  = new orderRHandle();
    		$remain = $or->getRemainData($did, $beginDate, $endDate);
    		if(count($remain)==0)
    		{
    				return true;
    		}
    		for($i=0; $i< count($remain); $i++)
    		{
    				if($remain[$i]['remain']=="0")
    				{
    						return false;
    				}
    		}
    		return true;
    }
    public function readRemainInfo($did, $beginDate, $endDate)
    {
    		$or  = new orderRHandle();
    		$remain = $or->getRemainData($did, $beginDate, $endDate);
    		return $remain;
    }
    public function submitOrder($uid, $did, $cid, $beginDate, $endDate)
    {
    		//设置两天之间的机器借出
    		$dw = new deviceWHandle();
    		$str1=strtotime($beginDate); 
				$str2=strtotime($endDate); 
				$daysBetween=($str2-$str1)/(3600*24)+1;
				
				//判断用户拥有的预约次数
    		$userR = new userRHandle();
    		$userW = new userWHandle();
    		if(!$userR->check_booktime($uid, $daysBetween) || !$userR->check_booktime_in_course($uid, $cid, $daysBetween))
    		{
    				return false;
    		}
    		//判断是否可以借
    		if(!$this->checkRemain($did, $beginDate, $endDate))
    		{
    				return false;
    		}
    		//更新预约次数
    		$userW->decrease_user_booktime($uid, $daysBetween);
    		$userW->decrease_user_course_booktime($uid, $cid, $daysBetween);
				for($i=0;$i<$daysBetween;$i++)
				{
						$nextDay = date( "Y-m-d",$str1+3600*24*$i);
						$dw->setDeviceUnavailable($did, $nextDay, '0');
				}
    		//预约信息插入预约表
    		$today =date("Y-m-d H:i:s");
    		$order=array('uid'=>$uid, 'did'=>$did, 'cid'=>$cid, 'start_date'=>$beginDate, 'end_date'=>$endDate, 'createdate'=>$today);
    		$orderw = new orderWHandle();
    		$orderw->insert_data($order);
    		return true;
    }
    public function undoOrder($oid, $uid)
    {
    		//获取预约信息
    		$orderR = new orderRHandle();
    		$orderW = new orderWHandle();
    		$deviceW = new deviceWHandle();
    		$orderInfo = $orderR->get_some_data(array('id'=>$oid, "uid"=>$uid, "pass"=>"0"));
    		if(count($orderInfo)<=0)
    		{
    				return false;
    		}
    		//恢复设备状态
    		$dw = new deviceWHandle();
    		$beginDate=$orderInfo[0]['start_date'];
    		$endDate=$orderInfo[0]['end_date'];
    		$str1=strtotime($beginDate); 
				$str2=strtotime($endDate); 
				$daysBetween=($str2-$str1)/(3600*24)+1;
    		$did=$orderInfo[0]['did'];
    		for($i=0;$i<$daysBetween;$i++)
				{
						$nextDay = date( "Y-m-d",$str1+3600*24*$i);
						$dw->setDeviceAvailable($did, $nextDay);
				}
    		//删除预约记录
    		$orderW->delete_order($oid);
    		//恢复用户预约次数
    		$userR = new userRHandle();
    		$userW = new userWHandle();
    		$cid=$orderInfo[0]['cid'];
    		$userW->increase_user_booktime($uid, $daysBetween);
    		$userW->increase_user_course_booktime($uid, $cid, $daysBetween);
    		return true;
    }
    public function getOrdersNumberByUserId($uid, $pass='')
    {
    		$or = new orderRHandle();
    		return $or->getOrdersNumberByUserId($uid, $pass);
    }
    public function getOrdersByUserId($uid, $pass='', $limit='')
    {
    		if($uid=='')
    		{
    				return array();
    		}
    		$or = new orderRHandle();
    		return $or->getOrdersByUserId($uid, $pass, $limit);
    }
    
    //function for admin
    public function getOrdersNumberByAdminId($adminId)
    {
    		$or = new orderRHandle();
    		return $or->getOrdersNumberByAdminId($adminId);
    }
    public function getOrdersByAdminId($uid, $limit='')
    {
    		if($uid=='')
    		{
    				return array();
    		}
    		$or = new orderRHandle();
    		return $or->getOrdersByAdminId($uid, $limit);
    }
    public function passOrder($id, $adminId)
    {
    		if($id=='' || $adminId=='')
    		{
    				return false;
    		}
    		$ow = new orderWHandle();
    		$today = date("Y-m-d H:i:s");
    		return $ow->passOrder($id, $adminId, $today);
    }
    public function rejectOrder($id, $adminId)
    {
    		if($id=='' || $adminId=='')
    		{
    				return false;
    		}
    		$ow = new orderWHandle();
    		$today = date("Y-m-d H:i:s");
    		return $ow->rejectOrder($id, $adminId, $today);
    }
    public function checkout($id)
    {
    		//借出
    		if($id=='')
    		{
    				return false;
    		}
    		$ow = new orderWHandle();
    		
    		$today = date("Y-m-d H:i:s");
    		return $ow->checkout($id,$today);
    }
    public function checkin($id)
    {
    		//归还
    		if($id=='')
    		{
    				return false;
    		}
    		$ow = new orderWHandle();
    		$today = date("Y-m-d H:i:s");
    		return $ow->checkin($id, $today);
    }
    public function searchByKeyword($keyword, $beginDate, $endDate, $type, $adminId)
    {
    		if(strlen($keyword)<3 && $beginDate=='' &&  $endDate=='' && ($type<-1 || $type>2))
    		{
    				return array();
    		}
    		if(strlen($keyword)<3)
    		{
    				$keyword='';
    		}
    		$cr = new orderRHandle();
    		return $cr->searchByKeyword($keyword, $beginDate, $endDate, $type, $adminId, "limit 0, 30");
    }
}
?>
